package pe.com.astech.web.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import pe.com.astech.web.bean.HerramientaBean;
import pe.com.astech.web.bean.TipoModuloBean;
import pe.com.astech.web.dao.TipoModuloDao;
import pe.com.astech.web.util.Constantes;

@Repository
public class TipoModuloDaoImpl implements TipoModuloDao {

private JdbcTemplate jdbcTemplate;
	
	private static final Log log = LogFactory.getLog(EmpleadoDaoImpl.class);

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	public List<TipoModuloBean> listarTipoModulo(String mensajeTrazabilidad) {
		String query = "select id_tipo_modulo,tipo from tipo_modulo where tipo like :tipo";
		List<TipoModuloBean> list = new ArrayList<TipoModuloBean>();
		log.info(mensajeTrazabilidad + " query : " + query);
			
			MapSqlParameterSource parameters = new MapSqlParameterSource();
		 	parameters.addValue("tipo", "%"+Constantes.VACIO+"%");
			
		 	NamedParameterJdbcTemplate namedParameterJdbcTemplate= new NamedParameterJdbcTemplate(jdbcTemplate);
			List<Map<String, Object>> usrRows = namedParameterJdbcTemplate.queryForList(query,parameters);
			
			for (Map<String, Object> usrRow : usrRows) {
				TipoModuloBean tipoModuloBean = new TipoModuloBean();
				tipoModuloBean.setId_tipo_modulo(String.valueOf(usrRow.get("id_tipo_modulo")));
				tipoModuloBean.setTipo(String.valueOf(usrRow.get("tipo")));
				log.info(mensajeTrazabilidad + " elementos : " + tipoModuloBean.toString());
				list.add(tipoModuloBean);
			}
		
		return list;
	}
	
}
